using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace ICP_MS.Plugin.Experiment;

public class FormInstrumentOpen : Form
{
	private IList<InstrumentConfig> configsAll = new List<InstrumentConfig>();

	private IList<InstrumentConfig> configs = new List<InstrumentConfig>();

	private IContainer components = null;

	private GroupBox groupBox1;

	private DataGridView dgvConfig;

	private Button btnOK;

	private Button btnCancel;

	private CheckBox chkHide;

	private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;

	private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;

	private DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;

	private DataGridViewTextBoxColumn dataGridViewTextBoxColumn4;

	private DataGridViewCheckBoxColumn colUse;

	private DataGridViewTextBoxColumn colTime;

	private DataGridViewTextBoxColumn colDescription;

	private DataGridViewTextBoxColumn colReserve;

	private DataGridViewTextBoxColumn colUser;

	public InstrumentConfig CurrentConfig { get; set; }

	public FormInstrumentOpen()
	{
		InitializeComponent();
	}

	private void dgvSettings_CellEndEdit(object sender, DataGridViewCellEventArgs e)
	{
	}

	private void dgvSettings_CellValueChanged(object sender, DataGridViewCellEventArgs e)
	{
		if (e.ColumnIndex != colUse.Index || e.RowIndex < 0)
		{
			return;
		}
		if (Convert.ToBoolean(dgvConfig[e.ColumnIndex, e.RowIndex].Value))
		{
			dgvConfig.CellValueChanged -= dgvSettings_CellValueChanged;
			foreach (InstrumentConfig config in configsAll)
			{
				config.Used = false;
			}
			CurrentConfig = configs[e.RowIndex];
			CurrentConfig.Used = true;
			dgvConfig.Invalidate();
			dgvConfig.CellValueChanged += dgvSettings_CellValueChanged;
		}
		else
		{
			CurrentConfig = null;
		}
	}

	private void btnOK_Click(object sender, EventArgs e)
	{
		foreach (InstrumentConfig cfg in configs)
		{
			if (cfg.Used)
			{
				CurrentConfig = cfg;
				break;
			}
		}
		base.DialogResult = DialogResult.OK;
	}

	private void btnCancel_Click(object sender, EventArgs e)
	{
		Close();
	}

	private void chkHide_CheckedChanged(object sender, EventArgs e)
	{
		LoadInstrumentConfigs();
	}

	private void dgvSettings_CellClick(object sender, DataGridViewCellEventArgs e)
	{
		if (e.ColumnIndex < 0 || e.RowIndex < 0 || !(dgvConfig.Columns[e.ColumnIndex].Name == "colUse"))
		{
			return;
		}
		dgvConfig[e.ColumnIndex, e.RowIndex].Value = true;
		foreach (DataGridViewRow dgr in (IEnumerable)dgvConfig.Rows)
		{
			if (dgr.Index != e.RowIndex)
			{
				dgr.Cells[e.ColumnIndex].Value = false;
			}
		}
	}

	private void FormInstrumentOpen_Load(object sender, EventArgs e)
	{
		LoadInstrumentConfigs();
		ApplyResources();
	}

	private void ApplyResources()
	{
		Text = Lang.Txt("FormInstrumentOpen.Title.Text");
		groupBox1.Text = Lang.Txt("FormInstrumentOpen.ConfigItems.Text");
		colDescription.HeaderText = Lang.Txt("FormInstrumentOpen.Columns.Description.Text");
		colReserve.HeaderText = Lang.Txt("FormInstrumentOpen.Columns.Param.Text");
		colTime.HeaderText = Lang.Txt("FormInstrumentOpen.Columns.DateTime.Text");
		colUse.HeaderText = Lang.Txt("FormInstrumentOpen.Columns.Select.Text");
		colUser.HeaderText = Lang.Txt("FormInstrumentOpen.Columns.User.Text");
		chkHide.Text = Lang.Txt("FormInstrumentOpen.HideSameConfig.Text");
		btnCancel.Text = Lang.Txt("FormInstrumentOpen.Button.Cancel.Text");
		btnOK.Text = Lang.Txt("FormInstrumentOpen.Button.OK.Text");
	}

	private void BindConfigs()
	{
		dgvConfig.AutoGenerateColumns = false;
		if (dgvConfig.DataSource == null)
		{
			dgvConfig.DataSource = configs;
			return;
		}
		BindingContext[dgvConfig.DataSource].SuspendBinding();
		dgvConfig.DataSource = null;
		dgvConfig.DataSource = configs;
		BindingContext[dgvConfig.DataSource].ResumeBinding();
	}

	private void LoadInstrumentConfigs()
	{
		IList<InstrumentConfig> cfgs = DataBase.Instance.GetAllInstrumentConfigs();
		if (cfgs != null)
		{
			configsAll = cfgs;
			if (chkHide.Checked)
			{
				configs = cfgs.Distinct(new InstrumentConfigCompare()).ToList();
			}
			else
			{
				configs = cfgs;
			}
		}
		BindConfigs();
	}

	private void dgvConfig_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
	{
		if (e.ColumnIndex == colTime.Index && e.RowIndex >= 0)
		{
			dgvConfig[colReserve.Index, e.RowIndex].Value = Lang.Txt("FormInstrumentOpen.DoubleClick.Text");
		}
	}

	private void dgvConfig_CurrentCellDirtyStateChanged(object sender, EventArgs e)
	{
		if (dgvConfig.IsCurrentCellDirty)
		{
			dgvConfig.CommitEdit(DataGridViewDataErrorContexts.Commit);
		}
	}

	private void FormInstrumentOpen_FormClosing(object sender, FormClosingEventArgs e)
	{
		DataBase.Instance.SaveInstrumentConfigs(configsAll);
	}

	private void DeleteSelectedConfigs(List<int> index)
	{
		List<InstrumentConfig> cfgs = new List<InstrumentConfig>();
		foreach (int row in index)
		{
			cfgs.Add(configs[row]);
		}
		foreach (InstrumentConfig cfg in cfgs)
		{
			if (DataBase.Instance.DeleteInstrumentConfig(cfg))
			{
				configs.Remove(cfg);
				if (CurrentConfig == cfg)
				{
					CurrentConfig = null;
				}
			}
		}
		BindConfigs();
	}

	private void dgvConfig_MouseDown(object sender, MouseEventArgs e)
	{
		if (e.Button != MouseButtons.Right || dgvConfig.SelectedCells.Count <= 0)
		{
			return;
		}
		List<int> index = new List<int>();
		foreach (DataGridViewCell item in dgvConfig.SelectedCells)
		{
			if (item.RowIndex >= 0)
			{
				index.Add(item.RowIndex);
			}
		}
		index = index.Distinct().ToList();
		ContextMenu menu = new ContextMenu();
		menu.MenuItems.Add(Lang.Txt("FormInstrumentOpen.Menu.Delete.Text"), delegate
		{
			DeleteSelectedConfigs(index);
		});
		menu.Show(dgvConfig, e.Location);
	}

	private void dgvConfig_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
	{
		if (e.ColumnIndex >= 0 && dgvConfig.Columns[e.ColumnIndex].Name == "colReserve")
		{
			FormInstrumentConfig formInstrumentConfig = new FormInstrumentConfig(configs[e.RowIndex].Time);
			formInstrumentConfig.ShowDialog();
		}
	}

	private void dgvConfig_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
	{
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
		this.groupBox1 = new System.Windows.Forms.GroupBox();
		this.dgvConfig = new System.Windows.Forms.DataGridView();
		this.colUse = new System.Windows.Forms.DataGridViewCheckBoxColumn();
		this.colTime = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.colDescription = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.colReserve = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.colUser = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.btnOK = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		this.chkHide = new System.Windows.Forms.CheckBox();
		this.dataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.dataGridViewTextBoxColumn2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.dataGridViewTextBoxColumn3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.dataGridViewTextBoxColumn4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.groupBox1.SuspendLayout();
		((System.ComponentModel.ISupportInitialize)this.dgvConfig).BeginInit();
		base.SuspendLayout();
		this.groupBox1.Controls.Add(this.dgvConfig);
		this.groupBox1.Location = new System.Drawing.Point(12, 12);
		this.groupBox1.Name = "groupBox1";
		this.groupBox1.Size = new System.Drawing.Size(626, 232);
		this.groupBox1.TabIndex = 0;
		this.groupBox1.TabStop = false;
		this.groupBox1.Text = "配置列表";
		this.dgvConfig.AllowUserToAddRows = false;
		this.dgvConfig.AllowUserToDeleteRows = false;
		this.dgvConfig.BackgroundColor = System.Drawing.SystemColors.Window;
		dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
		dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
		dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
		dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
		dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
		this.dgvConfig.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
		this.dgvConfig.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
		this.dgvConfig.Columns.AddRange(this.colUse, this.colTime, this.colDescription, this.colReserve, this.colUser);
		this.dgvConfig.Dock = System.Windows.Forms.DockStyle.Fill;
		this.dgvConfig.GridColor = System.Drawing.SystemColors.Window;
		this.dgvConfig.Location = new System.Drawing.Point(3, 17);
		this.dgvConfig.Name = "dgvConfig";
		this.dgvConfig.RowTemplate.Height = 23;
		this.dgvConfig.Size = new System.Drawing.Size(620, 212);
		this.dgvConfig.TabIndex = 0;
		this.dgvConfig.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(dgvSettings_CellClick);
		this.dgvConfig.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(dgvSettings_CellEndEdit);
		this.dgvConfig.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(dgvConfig_CellFormatting);
		this.dgvConfig.CellMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(dgvConfig_CellMouseClick);
		this.dgvConfig.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(dgvConfig_CellMouseDoubleClick);
		this.dgvConfig.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(dgvSettings_CellValueChanged);
		this.dgvConfig.CurrentCellDirtyStateChanged += new System.EventHandler(dgvConfig_CurrentCellDirtyStateChanged);
		this.dgvConfig.MouseDown += new System.Windows.Forms.MouseEventHandler(dgvConfig_MouseDown);
		this.colUse.DataPropertyName = "Used";
		this.colUse.HeaderText = "选择";
		this.colUse.Name = "colUse";
		this.colUse.ToolTipText = "选择配置并应用到当前";
		this.colUse.Width = 50;
		this.colTime.DataPropertyName = "Time";
		this.colTime.HeaderText = "时间/日期";
		this.colTime.Name = "colTime";
		this.colTime.ReadOnly = true;
		this.colTime.Width = 150;
		this.colDescription.DataPropertyName = "Description";
		this.colDescription.HeaderText = "说明";
		this.colDescription.Name = "colDescription";
		this.colDescription.ReadOnly = true;
		this.colDescription.Width = 160;
		this.colReserve.HeaderText = "参数";
		this.colReserve.Name = "colReserve";
		this.colReserve.ReadOnly = true;
		this.colUser.DataPropertyName = "User";
		this.colUser.HeaderText = "用户";
		this.colUser.Name = "colUser";
		this.colUser.ReadOnly = true;
		this.btnOK.Location = new System.Drawing.Point(157, 264);
		this.btnOK.Name = "btnOK";
		this.btnOK.Size = new System.Drawing.Size(75, 23);
		this.btnOK.TabIndex = 1;
		this.btnOK.Text = "确定";
		this.btnOK.UseVisualStyleBackColor = true;
		this.btnOK.Click += new System.EventHandler(btnOK_Click);
		this.btnCancel.Location = new System.Drawing.Point(391, 264);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 2;
		this.btnCancel.Text = "取消";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.chkHide.AutoSize = true;
		this.chkHide.Location = new System.Drawing.Point(422, 11);
		this.chkHide.Name = "chkHide";
		this.chkHide.Size = new System.Drawing.Size(120, 16);
		this.chkHide.TabIndex = 3;
		this.chkHide.Text = "隐藏相同说明配置";
		this.chkHide.UseVisualStyleBackColor = true;
		this.chkHide.CheckedChanged += new System.EventHandler(chkHide_CheckedChanged);
		this.dataGridViewTextBoxColumn1.DataPropertyName = "Time";
		this.dataGridViewTextBoxColumn1.HeaderText = "时间/日期";
		this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1";
		this.dataGridViewTextBoxColumn1.ReadOnly = true;
		this.dataGridViewTextBoxColumn1.Width = 120;
		this.dataGridViewTextBoxColumn2.DataPropertyName = "User";
		this.dataGridViewTextBoxColumn2.HeaderText = "用户";
		this.dataGridViewTextBoxColumn2.Name = "dataGridViewTextBoxColumn2";
		this.dataGridViewTextBoxColumn2.ReadOnly = true;
		this.dataGridViewTextBoxColumn2.Width = 80;
		this.dataGridViewTextBoxColumn3.DataPropertyName = "Description";
		this.dataGridViewTextBoxColumn3.HeaderText = "说明";
		this.dataGridViewTextBoxColumn3.Name = "dataGridViewTextBoxColumn3";
		this.dataGridViewTextBoxColumn3.ReadOnly = true;
		this.dataGridViewTextBoxColumn3.Width = 160;
		this.dataGridViewTextBoxColumn4.HeaderText = "参数..";
		this.dataGridViewTextBoxColumn4.Name = "dataGridViewTextBoxColumn4";
		this.dataGridViewTextBoxColumn4.ReadOnly = true;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(647, 322);
		base.Controls.Add(this.chkHide);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.btnOK);
		base.Controls.Add(this.groupBox1);
		base.MaximizeBox = false;
		base.MinimizeBox = false;
		base.Name = "FormInstrumentOpen";
		base.ShowIcon = false;
		this.Text = "仪器配置选择";
		base.FormClosing += new System.Windows.Forms.FormClosingEventHandler(FormInstrumentOpen_FormClosing);
		base.Load += new System.EventHandler(FormInstrumentOpen_Load);
		this.groupBox1.ResumeLayout(false);
		((System.ComponentModel.ISupportInitialize)this.dgvConfig).EndInit();
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
